/*
 * 文件名：YellowPageServiceImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年3月29日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.news.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.news.YellowPage;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.news.YellowPageDao;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.news.IYellowPageService;
import com.zcsy.manage.service.system.impl.SystemComService;

/**
 * @Title: YellowPageServiceImpl.java
 * @Package： com.zcsy.manage.service.news.impl 
 * @Description: 便民黄页信息service层接口实现类
 *
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年4月12日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class YellowPageServiceImpl extends SystemComService implements IYellowPageService{

	Pattern phoneReg = Pattern.compile("^(13[0-9]|15[0-9]|18[0-9]|14[578])[0-9]{8}$");
	Pattern number = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
	
	/*public static void main(String[] args) {
		Pattern phoneReg = Pattern.compile("^(13[0-9]|15[0-9]|18[0-9]|14[578])[0-9]{8}$");
		
		String string = "13986106142";
		
		System.out.println(phoneReg.matcher(string).matches());
	}*/
	
	@Autowired
	private YellowPageDao yellowPageDao;
	
	@Autowired
	private ICellInfoService CellInfoService;
 
	@Override
	public int insert(YellowPage yellowPage) {
		return yellowPageDao.insert(yellowPage);
	}

	@Override
	public int update(YellowPage yellowPage) {
		YellowPage oldPage = yellowPageDao.selectByPrimaryKey(yellowPage.getId());
		
		oldPage.setPageName(yellowPage.getPageName());
		oldPage.setCellId(yellowPage.getCellId());
		oldPage.setPhone0(yellowPage.getPhone0());
		oldPage.setPhone1(yellowPage.getPhone1());
		oldPage.setAddr(yellowPage.getAddr());
		oldPage.setSort(yellowPage.getSort());
		oldPage.setUpdateTime(yellowPage.getUpdateTime());
		
		return yellowPageDao.updateByPrimaryKey(oldPage);
	}

	@Override
	public int delete(String id) {
		return yellowPageDao.deleteByPrimaryKey(id);
	}

	@Override
	public YellowPage selectByPrimaryKey(String id) {
		return yellowPageDao.selectByPrimaryKey(id);
	}

	@Override
	public List<YellowPage> selectByKeyword(YellowPage yellowPage) {
		return yellowPageDao.selectByKeyword(yellowPage);
	}
	
	public List<YellowPage> selectBySearchKey(YellowPage yellowPage) {
	    return yellowPageDao.selectBySearchKey(yellowPage);
	}

    @Override
    public int importYellowPage(List<List> list, String cellId, User user) {
        StringBuffer message = new StringBuffer();
        int successCount = 0;
        int flag = Constants.SQL_RESULT_ERROR;
        String name ="";
        
        if (CollectionUtils.isNotEmpty(list)) {
            // 获取网优中心会审的信息
            for (List result : list) {
                @SuppressWarnings("unchecked")
                ArrayList<String> obj = (ArrayList<String>) result;
                
/*                CellHouse house = new CellHouse();
                house.setCellId(cellId);
                house.setRegionName((String) result.get(2));
                house.setBuildingName((String) result.get(3));
                house.setUnitName((String) result.get(4));
                name = (String) result.get(5);
                name = name.substring(0,name.indexOf("."));
                house.setName(name);
                List<CellHouse> houseList = cellHouseService.selectForImport(house);
                house = houseList.get(0);*/

                YellowPage yellowPage = new YellowPage();
                yellowPage.setCellId(cellId.toString());

                /** 根据便民黄页模板录入数据 **/
                yellowPage.setId(UniqueNumberUtils.get18UniqueNumber());
                yellowPage.setCellId(cellId);
                yellowPage.setPageName(yellowPage.getPageName());
                yellowPage.setCellName(yellowPage.getCellName());
                yellowPage.setPhone0(yellowPage.getPhone0());
                yellowPage.setPhone1(yellowPage.getPhone1());
                yellowPage.setAddr(yellowPage.getAddr());

                yellowPageDao.insert(yellowPage);
            }
            flag = Constants.SQL_RESULT_SUCCESS;
        }
        return flag;
    }

    @Override
    public String findBug(String cellId, List<List> list) {
    	String errorMsg = null;

        if(!list.isEmpty()) {   
            int size = list.size();
            for(int i = 2; i < (size+2); i++) {
                List record = list.get(i-2);
                //判断不为空
                if (StringUtils.isEmpty(record.get(1).toString())) {
                	errorMsg = "表格第"+i+"行名称不能为空！";
                    break;
                }
                if (StringUtils.isEmpty(record.get(2).toString())) {
                	errorMsg = "表格第"+i+"行小区名不能为空！";
                    break;
                }
                if (StringUtils.isEmpty(record.get(3).toString())) {
                	errorMsg = "表格第"+i+"行电话1不能为空！";
                    break;
                }
                if (StringUtils.isEmpty(record.get(5).toString())) {
                	errorMsg = "表格第"+i+"行地址不能为空！";
                    break;
                }
                CellInfo cellInfo = CellInfoService.selectByPrimaryKey(cellId);
                if (!cellInfo.getCellName().equals(record.get(2).toString())) {
                	errorMsg = "表格第"+i+"行小区名称应为当前小区名！";
                    break;
				}
                //判断电话1格式是否正确
                String p1 = record.get(3).toString();
            	Matcher phoneMatcher = phoneReg.matcher(record.get(3).toString());
            	if(!phoneMatcher.matches()){
            		errorMsg = "表格第"+i+"行电话1格式不正确！";
            		break;
            	}
            	//判断电话2格式是否正确
            	Matcher phone2Matcher = phoneReg.matcher(record.get(4).toString());
            	if(!phoneMatcher.matches()){
            		errorMsg = "表格第"+i+"行电话2格式不正确！";
            		break;
            	}
            }
        } else {
            errorMsg = "无数据";
        }
        return errorMsg;
    }

	@Override
	public int updateSort(YellowPage yellowPage) {
		return yellowPageDao.updateSort(yellowPage);
	}

	@Override
	public List<YellowPage> getSomeListBySort(String cellId, Integer limit) {
		return yellowPageDao.getSomeListBySort(cellId, limit);
	}

	@Override
	public int insertAll(List<YellowPage> yellowPages) {
		return yellowPageDao.insertAll(yellowPages);
	}


}
